<?php

class Userpanel extends Controller {
	function Userpanel()
	{
		parent::Controller();
		$settings = $this->sessionhelper->getSettings();
		if (!isset($settings['userid']))
		{
			//print_r($settings);
			redirect("/");
			die();
		}
			
	}
	
	function index()
	{
		$this->load->view('userpanel_view_menu');
	}
	function addorg()
	{
		if (isset($_POST['submit']))
		{
			$orgobj = new Org();
			$orgobj->where("orgname", $_POST['org'])->get();
			if (count($orgobj->all) > 0)
			{
				redirect("addorg");
			} else {
				$u = new User();
				$orgnew = new Org();
				$orgnew->orgname = $_POST['org'];
				$orgnew->save();
				$u->getUser()->save($orgnew);
				redirect("/userpanel");
			}
		}
		$this->load->view('addorg_view');
	}
	function delorg($id=0, $confirm="")
	{
		$u = new User();
		if ($id != 0)
		{
			$org = new Org();
			$org->where("id", $id)->get();
			if ($confirm=="y")
			{
				$org->delete();
				$u->getUser()->delete($org);
				redirect("/userpanel");
			} else {
				$data["org"] = $org;
				$this->load->view('delorg_confirm_view', $data);
			}
		} else {
			$data['orgs'] = $u->getUser()->org->get()->all;
			$this->load->view('delorg_view', $data);
		}
	}
	function addrule()
	{
		if (isset($_POST['submit']))
		{
			$org = new Org();
			$org->where("id", $_POST['org'])->get();
			if (count($org->all) == 0)
			{
				
				redirect("/userpanel/addrule");
			} else {
				$rule = new Rule();
				$rule->rule = $_POST['rule'];
				$rule_search = new Rule();
				$rule_search->where("id", $_POST['rules'])->get();
				if ($rule_search->result_count() > 0)
				{
					$rule->position = $rule_search->position + 1;
					$rule->save($org);
					redirect("/userpanel/");
				} else {
					$rule->position = 0;
					$rule->save($org);
					redirect("/userpanel/");
				}
			}
		} else {
			$user = new User();
			$data["orgs"] = $user->getUser()->org->get()->all;
			$this->load->view('addrule_view', $data);
		}
	}
	function bulkupload($process="0")
	{
		//@todo: Rewrite bulkupload
		$uobj = new User();
		$user = $uobj->getUser();
		$buobj = new Bulkupload();
		if ($process == "0")
		{
			if (isset($_POST['submit']))
			{
				if (file_exists($_FILES["bu"]["tmp_name"]))
				{ 
					$budata = base64_encode(file_get_contents($_FILES["bu"]["tmp_name"]));
					$buobj->user_id = $user->id;
					$buobj->data = $budata;
					$buobj->filename = $_FILES["bu"]["name"];
					$buobj->save();
				}
			}
		} else {
			$buobj2 = new Bulkupload();
			$buobj2->where("id", $process)->get();
			if ((count($buobj2->all) > 0) && ($buobj2->user_id == $user->id))
			{
				$budata = base64_decode($buobj2->data);
				$budata_arr = explode(PHP_EOL, $budata);
				//print_r($budata_arr);
				$header = $budata_arr[0];
				$header_arr = explode(",", $header);
				array_shift($budata_arr);
				$rule_arr = array();
				foreach($budata_arr as $burow)
				{
					if ($burow != "")
					{
						$row = explode(",", $burow);
						//foreach($header as $head)
						$rule_tmp_arr = array();
						for($i = 0; $i < count($header_arr); $i++)
						{
							$rule_tmp_arr[$header_arr[$i]] = $row[$i];
						}
						$rule_arr[] = $rule_tmp_arr;
					}
				}
				//print_r($rule_arr);
				foreach($rule_arr as $rul)
				{
					$newrule = new Rule();
					$org = new Org();
					foreach($rul as $key=>$val)
					{
						switch ($key)
						{
							case "chain":
							case "protocol":
							case "limit":
							case "cstate":
							case "action":
								$tmpname = ucfirst($key);
								$tmpnamecol = $key . "_id";
								$tmpvar = new $tmpname;
								$tmpvar->where($key, $val)->get();
								$newrule->$tmpnamecol = $tmpvar->id;
							case "org":
								$org->where("orgname", $val)->get();
								break;
							case "port":
							case "source_addr":
							case "destation_port":
							case "limit_duration":
							case "position":
							case "conntrack":
							case "iprange":
							case "comment":
								$newrule->$key = $val;
								break;
						}												
					}
					$newrule->user_id = $user->id;
					$newrule->save($org);
					//$org->save($newrule);
				}
			}
		}
		$data["bulkuploads"] = $buobj->where("user_id", $user->id)->get()->all;
		$this->load->view('addbu_view', $data);
	}
}

?>